package com.lhr.common.support;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.lhr.common.util.StringUtil;

public class EasyUITreeGridToStringStyle extends JSONToStringStyle {
	private static final long serialVersionUID = 1L;
	private String parentFieldName = "parentId";

	public EasyUITreeGridToStringStyle() {
		super();
	}

	public EasyUITreeGridToStringStyle(String parentFieldName) {
		super();
		this.parentFieldName = parentFieldName;
	}

	@Override
	public void appendEnd(StringBuffer buffer,
			Object object) {
		Object value = null;
		try {
			String methodName = StringUtil.buildGetMethod(parentFieldName);
			Method method = object.getClass().getMethod(methodName, new Class[] {});
			value = method.invoke(object);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		} catch (IllegalArgumentException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e);
		}

		buffer.append("\"_parentId\":");
		buffer.append("\"");
		if (value != null)
			buffer.append(value);
		buffer.append("\"");
		super.appendEnd(buffer, object);
	}

}
